package com.xixi.server;

import com.xixi.model.userMessage;
import com.xixi.model01.Student;
import com.xixi.model01.StudentMessage;

import java.io.IOException;
import java.util.List;

//编程实现服务器的主功能
public class ServerView {
    //使用合成复用原则
    private ServerInitClose serverInitClose;
    private serverDao sd;

//通过构造方法实现成员变量的初始化

    public ServerView(ServerInitClose serverInitClose, serverDao sd) {
        this.serverInitClose = serverInitClose;
        this.sd = sd;
    }
   /* public ServerView(ServerInitClose serverInitClose){
        this.serverInitClose=serverInitClose;
    }*/

    //自定义成员方法实现客户端发来消息的接受并处理      接收接受并打印
    public void serverReceive() throws IOException, ClassNotFoundException {
        // userMessage tem= serverInitClose.getObjectInputStream().readObject();
        userMessage tem = (userMessage) serverInitClose.getObjectInputStream().readObject();//这里也是抛出异常
        System.out.println("接收到的消息是：" + tem);
        //调用方法实现管理员账号和密码信息的校验
        if (sd.serverManagerCheck(tem.getUser())) {
            tem.setType("success");
        } else {
            tem.setType("fail");
        }
        //这里是将校验结果发送给客户端
        serverInitClose.getObjectOutputStream().writeObject(tem);
        System.out.println("服务器发送校验结果成功");
        if (sd.serverManagerCheck(tem.getUser())) {
            while (true) {
                tem = (userMessage) serverInitClose.getObjectInputStream().readObject();
                System.out.println("接收到的消息是：" + tem);
                if (tem.getType().equals("studentManager")) {//StudentMessage
                    while (true) {
                        StudentMessage studentMessage = (StudentMessage) serverInitClose.getObjectInputStream().readObject();
                        System.out.println("接收到的消息是：" + studentMessage);
                        if (studentMessage.getType().equals("addStudent")) {
                            Student student = studentMessage.getStudent();
                            List<Student> studentList = serverInitClose.getStudentList();
                            // List<Student> studentList=(List<Student>)serverInitClose.getObjectInputStream().readObject();
                            for (int i = 0; i < studentList.size(); i++) {
                                if (studentList.get(i).getName().equals(student.getName())) {
                                    studentMessage = new StudentMessage("fail", null);
                                    serverInitClose.getObjectOutputStream().writeObject(studentMessage);
                                    continue;
                                }
                            }
                            studentList.add(student);
                            studentMessage = new StudentMessage("success", null);
                            serverInitClose.getObjectOutputStream().writeObject(studentMessage);
                        } else if (studentMessage.getType().equals("getAllStudent")) {
                            //给客户端发送所有学员信息
                            serverInitClose.getObjectOutputStream().reset();
                            serverInitClose.getObjectOutputStream().writeObject(serverInitClose.getStudentList());
                        } else if (studentMessage.getType().equals("stuexit")) {
                            serverInitClose.saveStuList();
                            break;
                        } else if (tem.getType().equals("testPaperManager")) {

                        } else if (tem.getType().equals("exit")) {
                            break;
                        }
                    }
                }
            }
        }


    }
}
